#include "demo.h"
#include "ui_demo.h"
#include <windows.h>
#include <QPainter>
#include <QTimer>
#include <QString>
#include <QDebug>
#include <QTime>
#include <QPixmap>
#include <QMessageBox>
#include <QClipboard>
int flag=0;
//红色代表被选中 绿色代表还没有选中
QString tmp = NULL;

//初始化tmp为当前按钮标题
bool m_flag=false;
QClipboard *clip;//新建剪切板，用于复制控制台的详细信息


QPainter *painter;
QTimer *timer;//Floyd计时器
QTimer *timer1;//Floyd前奏，遍历显示计时器，启动floyd计时器
QTimer *timer2;//控制控制台输出
QTimer *timer3;//Dj算法定时器
QTimer *timer4;//Dj算法所有路径
int tmp1=0;//临时可删
bool status;//临时可删
QTime time_sum;//运行总耗时间


int sp_point,lp_point;

#define big 0xFFFFF;//定义最大形如无穷

double mg[22][22];//记录最小路径值
int m;//顶点个数
int n;//边的个数
int path[22][22];//记录中间点

int path_gai[22];//Floyd中用于存储最短路径的数组，通过计时器循环取出
int path_gai_num=1;//Floyd中存储最短路径的数组的下标，通过后续循环增加


int sp_ID,lp_ID;//全局ID，及将按钮转换为对应的ID
int f_count=1;
QString all_button[22];//全局按钮Text，后续用于寻找按钮ID

QPushButton *b1;//按钮变量，用于连接
QPushButton *b2=NULL;

QPushButton *b_tmp=NULL;
int line_f=1,list_f=1;//初始化

bool all_floyd=false;//Floyd是否完成



bool all_dj=false;//

bool all_dj_lu=false;//DJ的所有有效路径


int path_dj_num=1;//Dijkstra下标范围

int path_dj[22];//Dijkstra中用于存储最短路径的数组

int dj_line=1;//全局计时器加数，用于判断是否读到下标

double sum_dj=0;//Dijkstra最短路径的总长度


int con_dj_num=1;//console前驱路径计数

int con_dj_lu_num=1;

int sum_dj_con=1;

int dj_lu_all_arr[300][100];

Demo::Demo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Demo)
{
    ui->setupUi(this);
    status=true;
    ui->img->installEventFilter(this);
    ui->mask->installEventFilter(this);

    //QPainter painter(ui->img);
    painter = new QPainter(ui->img);
    timer=new QTimer(this);
    timer1=new QTimer(this);
    timer3=new QTimer(this);
    timer2=new QTimer(this);
    timer4=new QTimer(this);
    er_button();
    clip = QApplication::clipboard();


  connect(timer1,&QTimer::timeout,[=]()mutable{//需要捕获变量
      all_floyd=true;
      QCoreApplication::processEvents();//处理事件
      if(list_f>21)
      {
          list_f=1;
          line_f++;
      }
      if(line_f>21)
      {
          all_floyd=false;
          timer1->stop();
          Floyd(ui->SP->text(),ui->LP->text());
          grab_img();
          timer->setInterval(500);
          timer->start(); 
      }
      if(mg[line_f][list_f]<0xFFFFF)
      {
          b1=querybutton_byid(line_f);
          b2=querybutton_byid(list_f);
          grab_mask();
      }
      list_f++;



  });

   connect(timer,&QTimer::timeout,[=]()mutable{//需要捕获变量
       m_flag=true;
       QCoreApplication::processEvents();
       if(f_count>path_gai_num)
       {
           f_count=1;
           m_flag=false;
           clip->setText(ui->console->toPlainText());
           ui->console->append("路径连接完毕，详细信息已经到剪切板");
           ui->total_ride->setText(QString::number(mg[sp_ID][lp_ID])+"公里");
           QString time_all=QString::number(time_sum.elapsed()/1000.0,'f',3);
           ui->time_test->setText(time_all+"秒");
           QMessageBox::information(NULL, "提示", "Floyd算法展示完毕，绘制耗时"+time_all+"秒");
           timer->stop();
       }
       else
       {
           b1=querybutton_byid(path_gai[f_count]);
           b2=querybutton_byid(path_gai[f_count+1]);
           if(f_count==1)
               b_tmp=b1;
           ui->console->append("当前连接路径："+b1->text()+"->"+b2->text());
           qDebug()<<b1->text();
           qDebug()<<b2->text();
           grab_mask();//捕捉mask
           f_count=f_count+1;
       }


   });
 connect(timer3,&QTimer::timeout,[=]()mutable{//
     all_dj=true;
     QCoreApplication::processEvents();
     if(dj_line>path_dj_num)
     {
         dj_line=1;
         all_dj=false;
         clip->setText(ui->console->toPlainText());
         ui->console->append("绘制结束，Dijkstra算法展示完毕");
         ui->console->append("详细信息已复制到剪切板");
         ui->total_ride->setText(QString::number(sum_dj)+"公里");
         QString time_all=QString::number(time_sum.elapsed()/1000.0,'f',3);
         ui->time_test->setText(time_all+"秒");
         QMessageBox::information(NULL, "提示", "Dijkstra算法展示完毕，绘制耗时"+time_all+"秒");
         timer3->stop();
     }
     else
     {
         b1=querybutton_byid(path_dj[dj_line]);
         b2=querybutton_byid(path_dj[dj_line+1]);
         if(dj_line==1)
             b_tmp=b1;
         ui->console->append("当前连接路径："+b1->text()+"->"+b2->text());
         qDebug()<<b1->text();
         qDebug()<<b2->text();
         grab_mask();//捕捉mask，便于画图
         dj_line=dj_line+1;
     }


 });
  connect(timer2,&QTimer::timeout,[=]()mutable{//控制台输出前驱路径，便于观察
      if(con_dj_num>path_dj_num)
      {
          timer2->stop();
          ui->console->append("建立完毕，准备动态绘制最短路径");
          grab_img();
          time_sum.start();
          timer3->setInterval(1000);
          timer3->start();
      }
      else
      {
          ui->console->append(QString::number(con_dj_num)+":"+querybutton_byid(path_dj[con_dj_num])->text()+"->"+querybutton_byid(path_dj[con_dj_num+1])->text());
          con_dj_num++;
      }




});

   connect(timer4,&QTimer::timeout,[=]()mutable{//绘制DJ算法的所有路径
       time_sum.start();
       all_dj_lu=true;
       if(con_dj_lu_num<sum_dj_con)
       {
           b1=querybutton_byid(dj_lu_all_arr[con_dj_lu_num][1]);
           b2=querybutton_byid(dj_lu_all_arr[con_dj_lu_num][2]);
           grab_mask();
       }
       else
       {
           all_dj_lu=false;//停止绘图
           timer4->stop();
           timer3->setInterval(500);
           timer3->start();
       }
        con_dj_lu_num++;
  });

 ui->console->append("本程序为最短路径算法演示而生");
}







void Demo::er_button(){
    all_button[1]="guangyuan_button";
    all_button[2]="bazhong_button";
    all_button[3]="dazhou_button";
    all_button[4]="guangan_button";
    all_button[5]="nanchong_button";
    all_button[6]="suining_button";
    all_button[7]="luzhou_button";
    all_button[8]="yibin_button";
    all_button[9]="xichang_button";
    all_button[10]="panzhihua_button";
    all_button[11]="kangding_button";
    all_button[12]="maerkang_button";
    all_button[13]="mianyang_button";
    all_button[14]="deyang_button";
    all_button[15]="chengdu_button";
    all_button[16]="ziyang_button";
    all_button[17]="neijiang_button";
    all_button[18]="zigong_button";
    all_button[19]="meishan_button";
    all_button[20]="leshan_button";
    all_button[21]="yaan_button";
}




void Demo::init(){//初始化
    sum_dj_con=1;
    con_dj_lu_num=1;
    line_f=list_f=1;
    for(int i=0;i<22;i++)
    {
        path_gai[i]=0;
        path_dj[i]=0;
    }
    path_gai_num=1;
    path_dj_num=1;
    for(int i=1;i<=21;i++)
    {
        for(int j=1;j<=21;j++)
        {
            mg[i][j]=big;
            path[i][j]=j;
        }
    }
    m=21;
    n=29;
    mg[1][1]=0;
    mg[2][2]=0;
    mg[3][3]=0;
    mg[4][4]=0;
    mg[5][5]=0;
    mg[6][6]=0;
    mg[7][7]=0;
    mg[8][8]=0;
    mg[9][9]=0;
    mg[10][10]=0;
    mg[11][11]=0;
    mg[12][12]=0;
    mg[13][13]=0;
    mg[14][14]=0;
    mg[15][15]=0;
    mg[16][16]=0;
    mg[17][17]=0;
    mg[18][18]=0;
    mg[19][19]=0;
    mg[20][20]=0;
    mg[21][21]=0;
    mg[2][1]=146.5;
    mg[5][1]=220.9;
    mg[6][1]=260.9;
    mg[13][1]=173.4;
    mg[1][2]=146.5;
    mg[3][2]=134.0;
    mg[2][3]=134.0;
    mg[4][3]=168.9;
    mg[3][4]=168.9;
    mg[5][4]=82.9;
    mg[1][5]=220.9;
    mg[4][5]=82.9;
    mg[1][6]=260.9;
    mg[7][6]=235.8;
    mg[6][7]=235.8;
    mg[8][7]=109.8;
    mg[18][7]=94.9;
    mg[7][8]=109.8;
    mg[9][8]=552.2;
    mg[18][8]=75.3;
    mg[20][8]=156.2;
    mg[8][9]=552.2;
    mg[10][9]=210.7;
    mg[21][9]=309.7;
    mg[9][10]=210.7;
    mg[11][10]=521.5;
    mg[10][11]=521.5;
    mg[12][11]=326.8;
    mg[11][12]=326.8;
    mg[13][12]=394.9;
    mg[21][12]=387.9;
    mg[1][13]=173.4;
    mg[12][13]=393.4;
    mg[14][13]=52.4;
    mg[13][14]=52.4;
    mg[15][14]=88.2;
    mg[14][15]=88.2;
    mg[16][15]=88.0;
    mg[19][15]=72.8;
    mg[15][16]=88.0;
    mg[17][16]=93.0;
    mg[18][17]=45.4;
    mg[7][18]=94.9;
    mg[8][18]=75.3;
    mg[17][18]=45.4;
    mg[15][19]=72.8;
    mg[20][19]=82.3;
    mg[21][19]=98.9;
    mg[8][20]=156.2;
    mg[19][20]=82.3;
    mg[9][21]=309.7;
    mg[12][21]=387.9;
    mg[19][21]=98.9;
    mg[16][17]=93.0;
    for(int j=0;j<300;j++)
    {
        for(int k=0;k<100;k++)
        {
            dj_lu_all_arr[j][k]=0;
        }

    }
}



void Demo::Floyd(QString sp,QString lp){//传入按钮
    init();
    sp_ID=get_button_id(sp);
    lp_ID=get_button_id(lp);
    int i=0,j=0,k=0;
    for(k=1;k<=m;k++)
    {
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(mg[i][j]>mg[i][k]+mg[k][j])
                {
                    mg[i][j]=mg[i][k]+mg[k][j];//更新最小路径
                    path[i][j]=path[i][k];//更新中间点
                }

            }
        }
    }
    ui->console->append("遍历完毕，准备输出和绘制详细信息");
    i=sp_ID;
    j=lp_ID;
    qDebug()<<"最小路径为："<<mg[i][j];
    k = path[i][j];
    path_gai[path_gai_num]=sp_ID;
    ui->console->append("起点："+querybutton_byid(i)->text());
    qDebug()<<"起点："<<querybutton_byid(i)->text();
    //qDebug()("path: %d", i);//打印起点
    while(k != j){
        path_gai_num++;
        ui->console->append("中间点："+querybutton_byid(k)->text());
        qDebug()<<"中间点："<<querybutton_byid(k)->text();
        path_gai[path_gai_num]=k;
        k = path[k][j];
    }
    qDebug()<<"终点："<<querybutton_byid(j)->text();
    ui->console->append("终点："+querybutton_byid(j)->text());
    path_gai[path_gai_num+1]=k;
    ui->console->append("输出信息完毕，开始以默认速度动态绘制连线");
}


void Demo::Dijkstra(QString sp,QString lp){
    init();
    int S[22] = { 0 };	//已求得DJ最短路径的终点的集合
    int adr1=get_button_id(sp);
    int adr2=get_button_id(lp);
    int min = 1;	//当前寻找到的最短路径的终点
    double D[22];	//它的每个分量D[i]表示当前所找到的从始点adr1到每个终点vi的最短路径的长度

    for(int i=0;i<22;i++)
    {
        D[i]=0;
    }
    int temp;//交换顺序，便于顺序输出，去掉即为逆序
    temp = adr1;
    adr1 = adr2;
    adr2 = temp;




    for(int i=0;i<22;i++)
    {
        S[i]=0;
    }



    double predecessor[22] = {0};	//存放顶点的前驱，方便输出最短路径

    for(int i=0;i<22;i++)
    {
        predecessor[i]=0;
    }
    ui->console->append("正在建立前驱，准备输出详细信息");
    for (int i = 1; i < 22; i++)	//初始化所有顶点的前驱都为起始点
        predecessor[i] = adr2;

    for (int j = 1; j < 22; j++)
        D[j] = mg[adr2][j];

    for (int i = 1; i < 22; i++) {
        for (int j = 1; j < 22; j++) {
            if (j == adr2)	//防止寻找到的路径的起始点和终点相同
                continue;
            if (D[min] + mg[min][j] < D[j]) {
                D[j] = D[min] + mg[min][j];
                predecessor[j] = min;	//	前驱的建立
            }
        }
        min = search_min(D, S, i);	//	寻找当前最短路径，返回最短路径的终点

        if (min == -1) {
            qDebug()<<"非连通图，最短路径查询失败\n";
            exit(0);
        }

        S[i] = min;		//编号为min的顶点作为最短路径的终点，所以把此顶点放入S中


        if (min == adr1)	//如果现在选择的最短路径的终点恰好是adr2，则停止寻找最短路径，减少时间复杂度
            break;


    }
    //输出最短路径的信息
    int pre = adr1;
    sum_dj=D[adr1];
    qDebug("路径总长度：%f\n",D[adr1]);

    path_dj[path_dj_num]=pre;

    qDebug()<<"起点："<<querybutton_byid(pre)->text();


    while (pre != adr2)
    {
        path_dj_num++;
        qDebug()<<"中间点："<<querybutton_byid(pre)->text();
        path_dj[path_dj_num]=pre;
        pre = predecessor[pre];
    }
    qDebug()<<"终点："<<querybutton_byid(adr2)->text();
    path_dj[path_dj_num+1]=adr2;
    int ks=1;
    while(S[ks]!=0)
    {
        ks++;
    }
    for(int j=ks-1;j>0;j--)
    {
        for(int k=ks-1;k>0;k--)
        {
            if(has_line(S[j],S[k]))
            {
                dj_lu_all_arr[sum_dj_con][1]=S[j];
                dj_lu_all_arr[sum_dj_con][2]=S[k];
                sum_dj_con++;//画线总数
            }
        }
    }
    qDebug()<<"总线条数："<<sum_dj_con;
}
//判断当前顶点是否在数组S中，也就是说是否已经被选择过
bool Demo::has_line(int a,int b){
    if(mg[a][b] < 0xFFFFF)
    {
        return true;
    }
    else
        return false;
}
bool Demo::has_selected(int S[], int i, int k) {
    for (int j = 1; j < k; j++)
        if (i == S[j])
            return true;
    return false;
}
//从数组D中选取最小值
int Demo::search_min(double D[], int S[], int k) {	//k为数组S中元素的个数，也就是说现在已经选了k条最短路径
    int min = -1;	//若为非连通图，则返回-1
    double m = big;
    for (int i = 1; i < 22; i++)
        if (!has_selected(S, i, k) && (D[i] < m)) {
            min = i;
            m = D[i];
        }
    return min;
}

QPushButton* Demo::querybutton_byid(int id ){
    QPushButton *button = Demo::findChild<QPushButton *>(all_button[id], Qt::FindDirectChildrenOnly);
    return button;

}







QString Demo::get_button_name(int id){
    QString str_city[22]={"广元","巴中","达州","广安","南充","遂宁","泸州","宜宾","西昌","攀枝花","康定","马尔康","绵阳","德阳","成都","资阳","内江","自贡","眉山","乐山","雅安"};
    return str_city[id-1];
}





int Demo::get_button_id(QString s){
    QString str_city[22]={"广元","巴中","达州","广安","南充","遂宁","泸州","宜宾","西昌","攀枝花","康定","马尔康","绵阳","德阳","成都","资阳","内江","自贡","眉山","乐山","雅安"};
    QString tmp=s;
    qDebug()<<tmp;
    for(int i=0;i<22;i++)
    {
        if(QString::compare(tmp,str_city[i])==0)
            return (i+1);
    }
    return -1;
}


void Demo::paint()//画图
{

    //QPainter painter(ui->img);
    //painter.setPen(QPen(Qt::black,5));
    // painter.drawLine(100,100,200,200);
    // painter.drawLine(755,182,830,230);
    set_line(ui->guangyuan_button,ui->bazhong_button);
    set_line(ui->bazhong_button,ui->dazhou_button);
    set_line(ui->dazhou_button,ui->guangan_button);
    set_line(ui->guangan_button,ui->nanchong_button);
    set_line(ui->suining_button,ui->luzhou_button);
    set_line(ui->luzhou_button,ui->yibin_button);
    set_line(ui->yibin_button,ui->xichang_button);
    set_line(ui->xichang_button,ui->panzhihua_button);
    set_line(ui->panzhihua_button,ui->kangding_button);
    set_line(ui->kangding_button,ui->maerkang_button);
    set_line(ui->maerkang_button,ui->mianyang_button);
    set_line(ui->mianyang_button,ui->guangyuan_button);
    set_line(ui->guangyuan_button,ui->nanchong_button);
    set_line(ui->guangyuan_button,ui->suining_button);
    set_line(ui->mianyang_button,ui->deyang_button);
    set_line(ui->deyang_button,ui->chengdu_button);
    set_line(ui->chengdu_button,ui->ziyang_button);
    set_line(ui->ziyang_button,ui->neijiang_button);
    set_line(ui->neijiang_button,ui->zigong_button);
    set_line(ui->zigong_button,ui->yibin_button);
    set_line(ui->zigong_button,ui->luzhou_button);
    set_line(ui->xichang_button,ui->yaan_button);
    set_line(ui->yaan_button,ui->maerkang_button);
    set_line(ui->yaan_button,ui->meishan_button);
    set_line(ui->meishan_button,ui->leshan_button);
    set_line(ui->leshan_button,ui->yibin_button);
    set_line(ui->chengdu_button,ui->meishan_button);
}



Demo::~Demo()
{
    delete ui;
}

void Demo::set_line(QPushButton *s1,QPushButton *s2)
{
    int x1,y1,x2,y2;

    painter->begin(ui->img);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(Qt::lightGray,4));
    x1=s1->x();
    x2=s2->x();
    y1=s1->y();
    y2=s2->y();
    painter->drawLine(x1-2,y1-2,x2-2,y2-2);//考虑画笔粗细
    painter->end();
}

void Demo::set_line_find(QPushButton *s1,QPushButton *s2)
{
    int x1,y1,x2,y2;

    painter->begin(ui->mask);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(Qt::yellow,4));
    x1=s1->x();
    x2=s2->x();
    y1=s1->y();
    y2=s2->y();
    painter->drawLine(x1-2,y1-2,x2-2,y2-2);//考虑画笔粗细
    painter->end();
}


void Demo::set_line_blue(QPushButton *s1,QPushButton *s2)
{

//    time_sum.start();
    int x1,y1,x2,y2;
    painter->begin(ui->mask);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(Qt::blue,4));
    x1=s1->x();
    x2=s2->x();
    y1=s1->y();
    y2=s2->y();
    painter->drawLine(x1-2,y1-2,x2-2,y2-2);//考虑画笔粗细
    painter->end();
//    qDebug()<<time_sum.elapsed()<<"ms";
}

void Demo::set_line_red(QPushButton *s1,QPushButton *s2)
{

//    time_sum.start();
    int x1,y1,x2,y2;
    painter->begin(ui->mask);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(Qt::red,4));
    x1=s1->x();
    x2=s2->x();
    y1=s1->y();
    y2=s2->y();
    painter->drawLine(x1-2,y1-2,x2-2,y2-2);//考虑画笔粗细
    painter->end();
//    qDebug()<<time_sum.elapsed()<<"ms";
}

void Demo::set_line_purple(QPushButton *s1,QPushButton *s2)
{
    int x1,y1,x2,y2;
    painter->begin(ui->mask);
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QPen(QColor(128,0,128),4));
    x1=s1->x();
    x2=s2->x();
    y1=s1->y();
    y2=s2->y();
    painter->drawLine(x1-2,y1-2,x2-2,y2-2);//考虑画笔粗细
    painter->end();
}



bool Demo::eventFilter(QObject *watched, QEvent *event)   //用过滤器eventFilter（）拦截QLabel中的QEvent::Paint事件
{

    if(watched ==ui->img && event->type() == QEvent::Paint)
    {
        paint();
    }
    if(watched == ui->mask&&event->type()==QEvent::Paint)
    {
        if(m_flag)
        {
            set_line_blue(b1,b2);
        }
        if(all_floyd)
        {
            set_line_find(b1,b2);
        }
        if(all_dj)
            set_line_red(b1,b2);
        if(all_dj_lu)
            set_line_purple(b1,b2);
    }

    return QWidget::eventFilter(watched,event);
}

void Demo::set_button(QPushButton *s,bool checked){//通用
    tmp=s->text();//设置当前标题
    //2021-11-26 增加设置全局id值
    if(checked)//没有被选中
    {
        s->setStyleSheet("border-radius:5px;background-color:green;color:green");
    }
    else//被选中
    {
        s->setStyleSheet("border-radius:5px;background-color:red;color:red");
        if(!flag)
        {
            ui->SP->setText(tmp);
            flag++;
        }
        else
        {
            ui->LP->setText(tmp);
            flag--;
        }
    }
}



void Demo::on_luzhou_button_clicked()
{

}


void Demo::on_luzhou_button_clicked(bool checked)//泸州
{
    set_button(ui->luzhou_button,checked);
}

void Demo::on_bazhong_button_clicked(bool checked)//巴中
{
    set_button(ui->bazhong_button,checked);
}


void Demo::on_dazhou_button_clicked(bool checked)//达州
{
    set_button(ui->dazhou_button,checked);
}


void Demo::on_deyang_button_clicked(bool checked)//德阳
{
    set_button(ui->deyang_button,checked);
}


void Demo::on_guangan_button_clicked(bool checked)//广安
{
    set_button(ui->guangan_button,checked);
}

void Demo::on_guangyuan_button_clicked(bool checked)//广元
{
    set_button(ui->guangyuan_button,checked);
}


void Demo::on_kangding_button_clicked(bool checked)//康定
{
    set_button(ui->kangding_button,checked);
}

void Demo::on_leshan_button_clicked(bool checked)//乐山
{
    set_button(ui->leshan_button,checked);

}

void Demo::on_maerkang_button_clicked(bool checked)//马尔康
{
    set_button(ui->maerkang_button,checked);
}

void Demo::on_meishan_button_clicked(bool checked)//眉山
{
    set_button(ui->meishan_button,checked);

}

void Demo::on_mianyang_button_clicked(bool checked)//绵阳
{
    set_button(ui->mianyang_button,checked);
}

void Demo::on_nanchong_button_clicked(bool checked)//南充
{
    set_button(ui->nanchong_button,checked);

}

void Demo::on_neijiang_button_clicked(bool checked)//内江
{
    set_button(ui->neijiang_button,checked);

}

void Demo::on_panzhihua_button_clicked(bool checked)//攀枝花
{

    set_button(ui->panzhihua_button,checked);

}

void Demo::on_suining_button_clicked(bool checked)//遂宁
{
    set_button(ui->suining_button,checked);

}

void Demo::on_xichang_button_clicked(bool checked)//西昌
{

    set_button(ui->xichang_button,checked);
}

void Demo::on_yaan_button_clicked(bool checked)//雅安
{
    set_button(ui->yaan_button,checked);

}

void Demo::on_yibin_button_clicked(bool checked)//宜宾
{
    set_button(ui->yibin_button,checked);
}

void Demo::on_zigong_button_clicked(bool checked)//自贡
{
    set_button(ui->zigong_button,checked);

}

void Demo::on_algorithm_1_clicked()//启动Floyd算法
{
//    sp_point=get_button_id(ui->SP->text());
//    lp_point=get_button_id(ui->LP->text());
//    qDebug()<<"起点ID："<<sp_point;
//    qDebug()<<"终点ID："<<lp_point;
    if(ui->LP->text()=="" && ui->LP->text()=="")
    {
       QMessageBox::information(NULL, "提示", "您并未选择起始点，请重新选择");
       return;
    }
    qDebug()<<f_count;
    ui->console->append("Floyd算法开始，正在进行完全遍历");
    init();
    grab_img();
    time_sum.start();
    timer1->setInterval(20);
    timer1->start();

}

void Demo::on_luzhou_button_pressed()
{

}

void Demo::on_chengdu_button_clicked(bool checked)//成都
{
    tmp=ui->chengdu_button->text();//设置当前标题
    if(checked)//没有被选中
    {
        ui->chengdu_button->setStyleSheet("border-radius:10px;background-color:green;color:green");
    }
    else//被选中
    {
        ui->chengdu_button->setStyleSheet("border-radius:10px;background-color:red;color:red");
        if(!flag)
        {
            ui->SP->setText(tmp);
            flag++;
        }
        else
        {
            ui->LP->setText(tmp);
            flag--;
        }
    }
}

void Demo::on_ziyang_button_clicked(bool checked)
{
    tmp=ui->ziyang_button->text();//设置当前标题
    if(checked)//没有被选中
    {
        ui->ziyang_button->setStyleSheet("border-radius:6px;background-color:green;color:green");
    }
    else//被选中
    {
        ui->ziyang_button->setStyleSheet("border-radius:6px;background-color:red;color:red");
        if(!flag)
        {
            ui->SP->setText(tmp);
            flag++;
        }
        else
        {
            ui->LP->setText(tmp);
            flag--;
        }
    }

}





void Demo::on_test_clicked()
{
    //通过设置标志符 update刷新实现重画
//    update();
//    m_flag=true;
//    tmp1=0;
//    timer->start();
//    ui->total_ride->setText("173.4 公里");
//set_line_blue(ui->chengdu_button,ui->ziyang_button);
    QPixmap Pix=QWidget::grab(QRect(QPoint(10,10),QSize(991,771)));
    ui->img->setPixmap(Pix);


}

void Demo::grab_img(){
    QPixmap pix=QPixmap::grabWidget(ui->img);//捕捉整个img图层，并且设置自身，避免因为位图缓存问题导致图形丢失
    ui->img->setPixmap(pix);
}

void Demo::grab_mask(){//捕捉Mask层区域，捕捉后设置下方img图片为捕捉图，继续绘画
    QPixmap pix=QWidget::grab(QRect(QPoint(10,10),QSize(991,771)));
    ui->img->setPixmap(pix);
}


void Demo::on_chengdu_button_clicked()
{

}

void Demo::on_clean_clicked()
{
    QPixmap clearPix = QPixmap();
    ui->img->setPixmap(clearPix);
    ui->mask->setPixmap(clearPix);
    ui->console->clear();
    ui->LP->setText("");
    ui->SP->setText("");
    ui->time_test->setText("");
    ui->total_ride->setText("");
    b2->setStyleSheet("border-radius:5px;background-color:green;color:green");
    b_tmp->setStyleSheet("border-radius:10px;background-color:green;color:green");
    QMessageBox::information(NULL, "提示", "控制台输出和Mask清除成功");
}

void Demo::on_test_2_clicked()
{




}

void Demo::on_algorithm_2_clicked()//启动Dijkstra算法
{
    qDebug()<<dj_line;
    if(ui->LP->text()=="" && ui->LP->text()=="")
    {
       QMessageBox::information(NULL, "提示", "您并未选择起始点，请重新选择");
       return;
    }
    ui->console->append("Dijkstra算法展示启动");
    init();
    ui->console->append("初始化完毕");
    Dijkstra(ui->LP->text(),ui->SP->text());
    grab_img();
    timer4->setInterval(100);
    timer4->start();

}


